package com.example.candy.netdemo1;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.tv);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                String info=msg.getData().getString("info");
                tv.setText(info);
            }
        };
    }

    public void startMyTCPServer(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    ServerSocket server=new ServerSocket(4444);
                    while(true){
                        Socket sk=server.accept();
                        InputStream in=sk.getInputStream();
                        InputStreamReader inReader=new InputStreamReader(in);
                        BufferedReader reader=new BufferedReader(inReader);
                        String line=reader.readLine();
                        //Log.i("CandyServer接收到的消息是",line);
                        Message msg=new Message();
                        Bundle b=new Bundle();
                        b.putString("info",line);
                        msg.setData(b);
                        handler.sendMessage(msg);

                        OutputStream out=sk.getOutputStream();
                        out.write(("Send Massege are "+line).getBytes());
                        out.close();
                        sk.close();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void sendToTCPServer(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    Socket sk=new Socket("192.168.1.104",4440);
                    OutputStream out=sk.getOutputStream();
                    out.write("I am CandyClient!\n".getBytes());
                    out.close();
                    sk.close();
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }.start();
    }

    public void startMyUDPServer(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    byte[] buf=new byte[1024];//开缓存区接收Client数据
                    DatagramSocket sk=new DatagramSocket(7770);//定义DatagramSocket用来发送或接收DatagramPacket，开放端口号7777
                    DatagramPacket p=new DatagramPacket(buf, 1024);//定义DatagramPacket用来获取buf缓存区
                    sk.receive(p);//Socket得到Packet p
                    byte[] data=p.getData();//得到收到的包中数据存放在data缓存区
                    String s =new String(data,0,p.getLength());//读取缓存区中字符放在字符数组中
                    Log.i("Candy","rev......."+s);//输出

                    //收到客户端消息后响应
                    InetAddress remoteAddr=p.getAddress();
                    int remotePort=p.getPort();
                    DatagramPacket sp=new DatagramPacket("shou dao.....".getBytes(), 13, remoteAddr, remotePort);
                    sk.send(sp);

                    sk.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void sendToUDPServer(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    DatagramSocket sk=new DatagramSocket();
                    String msg="I am Candy with UDP Client!";
                    InetAddress addr=InetAddress.getByName("192.168.1.104");

                    DatagramPacket p =new DatagramPacket(msg.getBytes(),msg.length(),addr,7770);
                    sk.send(p);

                    byte[] recBuf=new byte[1024];
                    DatagramPacket rp=new DatagramPacket(recBuf, 1024);
                    sk.receive(rp);
                    String recvMsg=new String(rp.getData(), 0, rp.getLength());
                    Log.i("Candy","from UDPServer....."+recvMsg);

                    sk.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
